package org.ty.rpc.core.serializer;

import java.util.HashMap;
import java.util.Map;

/**
 * 序列化器键名
 */
public interface SerializerKeys {

    String JDK = "jdk";
    String JSON = "json";
    String KRYO = "kryo";
    String HESSIAN = "hessian";

    /**
     * 序列化器工厂（用于获取序列化器对象）
     *
     * @author <a href="https://github.com/liyupi">程序员鱼皮</a>
     * @learn <a href="https://codefather.cn">编程宝典</a>
     * @from <a href="https://yupi.icu">编程导航知识星球</a>
     */
    public class SerializerFactory {

        /**
         * 序列化映射（用于实现单例）
         */
        private static final Map<String, Serializer> KEY_SERIALIZER_MAP = new HashMap<String, Serializer>() {{
            put(SerializerKeys.JDK, new JdkSerializer());
            put(SerializerKeys.JSON, new JsonSerializer());
            put(SerializerKeys.KRYO, new KryoSerializer());
            put(SerializerKeys.HESSIAN, new HessianSerializer());
        }};

        /**
         * 默认序列化器
         */
        private static final Serializer DEFAULT_SERIALIZER = KEY_SERIALIZER_MAP.get("jdk");

        /**
         * 获取实例
         *
         * @param key
         * @return
         */
        public static Serializer getInstance(String key) {
            return KEY_SERIALIZER_MAP.getOrDefault(key, DEFAULT_SERIALIZER);
        }

    }}